﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var a = "SELECT *FROM Students";
            var dt = DBHelper.GetDataTable(a);
            dataGridView1.DataSource = dt;
            //限制dataGridView某些行为
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        //增加操作
        private void button1_Click(object sender, EventArgs e)
        {
            EditForm edit = new EditForm();
            var res = edit.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var a = "SELECT *FROM Students";
                var dt = DBHelper.GetDataTable(a);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("Sorry,NO!!!","消息提示");
            }

        }
        //修改操作
        private void button2_Click(object sender, EventArgs e)
        {
            var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
            var age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
            var score = (int)dataGridView1.SelectedRows[0].Cells["Score"].Value;
            EditForm edit = new EditForm(id,name,age,score);
            var res = edit.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var a = "SELECT *FROM Students";
                var dt = DBHelper.GetDataTable(a);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("SOrry,NO!!!","消息提示");
            }
        }
        //删除操作
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var sql = string.Format("DELETE FROM Students WHERE Id={0}", id);
                var res = DBHelper.AddUpdate(sql);
                if (res == 1)
                {
                    MessageBox.Show("删除成功！！！", "消息提示");
                    var a = "SELECT *FROM Students";
                    var dt = DBHelper.GetDataTable(a);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("删除失败！！！", "消息提示");
                }
            }
            else
            {
                MessageBox.Show("未选择数据","消息提示");
            }
        }
        //查询操作
        private void button4_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var sql = string.Format("SELECT *FROM Students WHERE Name LIKE '%{0}%'",name);
            var dt = DBHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }
    }
}
